<?php
/*
 * @Date: 2021-01-30 20:11:19
 * @名称: 修改某个漫画自己的权限
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-03-01 00:30:53
 * @FilePath: /Site/app/useradmin/controller/Comics/User/ModifyYourOwnPermissions.php
 */

namespace app\useradmin\controller\Comics\User;

use app\BaseController;
use app\useradmin\controller\DataBaseController\LogController;
use think\facade\Db;

class ModifyYourOwnPermissions extends BaseController
{
    public function enter($user_info, $postData, $author_and_chinese_team_id)
    {
        $me_AuthorAndChineseTeam_info =  Db::table("author_and_chinese_team") //!查询我对这篇文章的信息
            ->where([
                "book_id" => $postData['bookId'],
                "user_id" => $user_info["_id"],
                "state" => 0,
            ])
            ->find();
        if (
            $postData['DecodeinitListUserInfo'] !=
            $postData['DecodeListUser_Info']
        ) { //!判断我的新id和原id一致 不一致就是修改了这个用户的所有者 如原来是张三拥有这个漫画 现在吧张三的信息改成李四的 那就不一致
            // ! 移交用户
            $selectAuthorAndChineseTeam =  Db::table('author_and_chinese_team')
                ->where([
                    "book_id" => $postData['bookId'],
                    "user_id" => $postData['jsonDecodeUserInfo']["user_id"],
                ])
                ->find();

            if ($selectAuthorAndChineseTeam) { //!添加的用户已在作者或著作列表里
                return errorJsonReturn('Change failed this user is already in the group', '更改失败该用户已在群组中');
            }
            if ($me_AuthorAndChineseTeam_info['author_or_chinese_team'] == 0) { //@ 判断我是不是创建者
                //!作者直接修改数据库   
                $update_state = Db::table('author_and_chinese_team') //更新用户数据
                    ->where([
                        "book_id" => $postData['bookId'],
                        "user_id" => $postData['jsonDecodeinitListUserInfo']["user_id"], //!被修改的用户的id
                        "state" => 0,
                    ])
                    ->update([
                        "user_id" => $postData['jsonDecodeUserInfo']["user_id"], //*新用户id
                        "insert_name_id" => $user_info["_id"],
                        "add_time" => date('Y-m-d H:i:s'),
                        "author_or_chinese_team" => $postData['initListType'],
                    ]);
                if ($update_state) {
                    $LogController = new LogController($this->app);
                    $LogController->insert_AACT_log(
                        $postData['jsonDecodeinitListUserInfo']["book_id"],
                        $postData['jsonDecodeinitListUserInfo']["user_id"],
                        $postData['jsonDecodeUserInfo']["user_id"],
                        $author_and_chinese_team_id,
                        "用户 " . $user_info["_id"] . " 将 " . $postData["jsonDecodeinitListUserInfo"]["user_id"] . " 移除了 并且替换为了 " . $postData["jsonDecodeUserInfo"]["user_id"],
                        1,
                        $user_info["_id"],
                        [
                            "implement_user_id" =>  $user_info['_id'],
                            "implement_time" => date("Y-m-d H:i:s"),
                        ]
                    );
                } else {
                    return errorJsonReturn('数据插入错误 错误行' . __LINE__ . "请联系站长解决 标识符：Pica_acg-author"); //!如果不是这两个其中的权限 则报错
                }
            } else {
                //*不是作者 添加审核列表 一旦添加审核丧失全部权利
                if ($postData['jsonDecodeinitListUserInfo']["user_id"] != $user_info['_id']) {
                    return errorJsonReturn('User permission error', '请刷新网页再试' . __LINE__); //!当用户已处于其他状态时候 
                }
                try {
                    Db::table('author_and_chinese_team')
                        ->where([
                            "book_id" => $postData['bookId'],
                            "user_id" => $postData['jsonDecodeinitListUserInfo']["user_id"], //!原用户id
                            "state" => 0,
                        ])
                        ->update([
                            "exp_state" => 8,
                            "state" => 1,
                            "is_deleted" => 1,
                        ]);
                    Db::table('author_and_chinese_team')
                        ->insert([
                            "book_id" => $postData['bookId'],
                            "user_id" => $postData["jsonDecodeUserInfo"]["user_id"],
                            "author_or_chinese_team" => 1,
                            "add_time" => date('Y-m-d H:i:s'),
                            "insert_name_id" => $postData['jsonDecodeinitListUserInfo']["user_id"],
                            "state" => 1,
                            "_id" => $author_and_chinese_team_id,
                            "exp_state" => 9, //0协作者请求删除协作者 1代漫画作者审核 2已删除 3转让自己的角色待审核 4已转让  5新加入待漫画作者审核 6漫画作者审核未通过 7自己退出项目 8移交的项目 9移入
                        ]);
                    $LogController = new LogController($this->app);
                    $logInsertState = $LogController->insert_AACT_log(
                        $postData['jsonDecodeinitListUserInfo']["book_id"],
                        $postData['jsonDecodeinitListUserInfo']["user_id"],
                        $postData['jsonDecodeUserInfo']["user_id"],
                        $author_and_chinese_team_id,
                        "用户 " . $user_info["_id"] . " 移交自己权限 到了" . $postData["jsonDecodeUserInfo"]["user_id"] . '身上',
                        0,
                        $user_info["_id"],

                    );
                    var_dump($logInsertState);
                    //code...
                } catch (\Throwable $th) {
                    //throw $th;
                    dump($th);
                }
            }
        } else {
            // ! 不移交用户
            if ($me_AuthorAndChineseTeam_info['author_or_chinese_team'] != 0 && $me_AuthorAndChineseTeam_info['exp_state']  == null) { //@判断我对这个漫画的权限 如果不是作者
                return errorJsonReturn('Insufficient authority',); //!如果不是这两个其中的权限 则报错
            }

            $selectAuthorAndChineseTeam =  Db::table('author_and_chinese_team')
                ->where([
                    "_id" => $postData['jsonDecodeUserInfo']["_id"],
                    "book_id" => $postData['jsonDecodeUserInfo']["book_id"],
                    "user_id" => $postData['jsonDecodeUserInfo']["user_id"],
                    "author_or_chinese_team" => $postData['jsonDecodeUserInfo']["role"],
                    "state" => 0,
                ])
                ->find();
            if ($selectAuthorAndChineseTeam) {
                if ($postData['jsonDecodeUserInfo']["role"] == $postData['initListType']) {
                    return errorJsonReturn('The new permission setting is consistent with the old permission',); //!如果不是这两个其中的权限 则报错
                }
                switch ($postData['jsonDecodeUserInfo']["role"]) { //!判断权限 老权限
                    case 0:
                        $OriginalPermissions = '创作者'; //!设置 老权限 为 创作者
                        break;
                    case 1:
                        $OriginalPermissions = '协作者'; //*设置 老权限 为 协作者
                        break;
                    default:
                        return errorJsonReturn('Permissions Setting Error', '请尝试重新请求或刷新'); //!如果不是这两个其中的权限 则报错
                        break;
                }
                switch ($postData['initListType']) { //!判断权限 新权限
                    case 0:
                        $NewPermissions = '创作者'; //!设置 新权限 为 创作者
                        break;
                    case 1:
                        $NewPermissions = '协作者'; //*设置 新权限 为 协作者
                        break;
                    default:
                        return errorJsonReturn('Permissions Setting Error', '请尝试重新请求或刷新'); //!如果不是这两个其中的权限 则报错
                        break;
                }



                try {
                    $LogController = new LogController($this->app);
                    $LogController->insert_AACT_log(
                        $postData['jsonDecodeUserInfo']["book_id"],
                        $postData['jsonDecodeUserInfo']["user_id"],
                        $postData['jsonDecodeUserInfo']["user_id"],
                        $author_and_chinese_team_id,
                        "权限变更由 <font  color='red'> $OriginalPermissions </font> 转换为 <font color='aqua'> $NewPermissions </font>",
                        1,
                        $user_info["_id"],
                        [
                            "implement_user_id" =>  $user_info['_id'],
                            "implement_time" => date("Y-m-d H:i:s"),
                        ]
                    );
                    $insertAuthorAndChineseTeam =   Db::table('author_and_chinese_team')
                        ->where([
                            "_id" => $postData['jsonDecodeUserInfo']["_id"],
                            "book_id" => $postData['jsonDecodeUserInfo']["book_id"],
                            "user_id" => $postData['jsonDecodeUserInfo']["user_id"],
                            "author_or_chinese_team" => $postData['jsonDecodeUserInfo']["role"],
                            "state" => 0,
                        ])
                        ->update([
                            "author_or_chinese_team" => $postData['initListType'],
                        ]);
                    if (!$insertAuthorAndChineseTeam) {
                        Db::table('author_and_chinese_team_operation')->where([
                            "apply_user_id" =>  $user_info['_id'],
                            "implement_user_id" =>  $user_info['_id'],
                            "book_id" => $postData['jsonDecodeUserInfo']["book_id"],
                            "user_id" => $postData['jsonDecodeUserInfo']["user_id"],
                            "new_user_id" => $postData['jsonDecodeUserInfo']["user_id"],
                            "_id" => $author_and_chinese_team_id,
                            "msg" => "权限变更由 <font  color='red'> $OriginalPermissions </font> 转换为 <font color='aqua'> $NewPermissions </font>",
                            "state" => 1,
                            "add_time" => date("Y-m-d H:i:s"),
                            "implement_time" => date("Y-m-d H:i:s"),
                        ])->delete();
                        return errorJsonReturn('数据插入错误 错误行' . __LINE__ . "请联系站长解决 操作已回滚 标识符：Pica_acg-author"); //!如果不是这两个其中的权限 则报错
                    }
                } catch (\Throwable $th) {
                    return errorJsonReturn('数据插入错误 错误行' . __LINE__ . "请联系站长解决 标识符：Pica_acg-author"); //!如果不是这两个其中的权限 则报错
                }
                return successJsonReturn([
                    "state" => "success",
                ]);
            } else {
                return errorJsonReturn('Invalid data refresh page retry');
            }
        }
    }
}
